/*
 * @Description: 新增登记弹窗
 * @Author: Rfan
 * @Date: 2022-11-23 16:56:05
 * @LastEditTime: 2023-01-17 18:01:07
 */

import { Button, Form, Modal } from 'antd';
import { useEffect } from 'react';
import { useAccessionContext } from '../../contexts/AccessionContext';
import AccessionForm from './common/AccessionForm';
import type { ICommonDialog } from '@/interfaces/global';
import type { IAddRegAccessionParams } from '@/api/registration/accession';

interface IAddTransferDialogProps extends ICommonDialog {}

const AddAccessionDialog = (props: IAddTransferDialogProps) => {
  const { visible, onClose } = props;
  const { addRegAccessionFunc } = useAccessionContext();
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      form.setFieldsValue({});
    }
  }, [visible]);

  const onCancel = () => {
    onClose();
  };

  // 提交表单
  const submitFunc = () => {
    form.submit();
  };

  // 新增档案类型
  const onFinish = (values: IAddRegAccessionParams) => {
    addRegAccessionFunc({ ...values }).then(() => {
      onClose();
    });
  };

  return (
    <Modal
      destroyOnClose
      title="新建登记条目"
      visible={visible}
      width={600}
      onCancel={onCancel}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          保存
        </Button>
      ]}
    >
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        preserve={false}
        initialValues={{
          delete: 0,
          field_type: 0,
          field_kind: 1,
          default_value: '',
          allowed_dup: true,
          allowed_null: true
        }}
        form={form}
        onFinish={onFinish}
      >
        <AccessionForm visible={visible} />
      </Form>
    </Modal>
  );
};

export default AddAccessionDialog;
